package com.swordxu.yim.server.loginserver;

import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.swordxu.yim.server.dbproxy.model.Session;
import com.swordxu.yim.server.dbproxy.model.User;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import org.apache.log4j.Logger;

import javax.net.ssl.SSLException;
import java.security.cert.CertificateException;

/**
 * 登录服务器
 * Created by user063 on 2014/12/23.
 */
public class LoginServer {
    protected Logger logger = Logger.getLogger(LoginServer.class);
    static final boolean SSL = System.getProperty("ssl") != null;
    static final int PORT = 8000;
    public static boolean isSSL;

    /**
     * 启动登录服务器
     */
    public void startServer() throws SSLException, CertificateException, InterruptedException {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        SslContext sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());

        // Configure the server.
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new LoginServerInitializer(sslCtx));
            b.option(ChannelOption.SO_KEEPALIVE, true);
            Channel ch = b.bind("192.168.1.63", PORT).sync().channel();

//            //加载数据代理插件
//            C3p0Plugin c3p0Plugin = new C3p0Plugin("jdbc:mysql://192.168.59.103/db_yim?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull", "root", "abc123");
//            c3p0Plugin.start();
//            ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
//            arp.addMapping("t_user", User.class);
//            arp.addMapping("t_session", Session.class);
//            arp.start();
            ch.closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    /**
     * 关闭登录服务器
     */
    public void closeServer() {

    }

    public static void main(String args[]) {
        LoginServer ls = new LoginServer();
        try {
            ls.startServer();
        } catch (SSLException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
